home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume6 / rpn < prev    next >
Encoding:
Text File  |  1989-03-07  |  7.1 KB  |  286 lines

  1. Newsgroups: comp.sources.misc
  2. Message-Id: <8902242245.AA02193@uunet.UU.NET>
  3. Subject: v06i061: rpn - A reverse polish notation calculator
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Reply-To: mtymp01@ux.acss.umn.edu
  6.  
  7. Posting-number: Volume 6, Issue 61
  8. Submitted-by: mtymp01@ux.acss.umn.edu
  9. Archive-name: rpn
  10.  
  11. [Notes:  (1) copyrighted (and rejected by r$); (2) uses ANSI C (pcc users
  12. beware!).  ++bsa]
  13.  
  14. [I know dc exists, I like this better, it has optional display of
  15.         top of stack]
  16.  
  17. #! /bin/sh
  18. # This is a shell archive.  Remove anything before this line, then unpack
  19. # it by saving it into a file and typing "sh file".  To overwrite existing
  20. # files, type "sh file -c".  You can also feed this as standard input via
  21. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  22. # will see the following message at the end:
  23. #        "End of shell archive."
  24. # Contents:  Makefile README rpn.1 rpn.c
  25. # Wrapped by mtymp01@ux.acss.umn.edu on Fri Feb 24 16:47:38 1989
  26. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  27. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  28.   echo shar: Will not clobber existing file \"'Makefile'\"
  29. else
  30. echo shar: Extracting \"'Makefile'\" \(159 characters\)
  31. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  32. XDESTMAN=/usr/man/man1
  33. XDESTDIR=/usr/bin
  34. X
  35. Xrpn: rpn.o
  36. X    cc -o rpn rpn.o
  37. X
  38. Xrpn.o: rpn.c
  39. X    cc -c rpn.c
  40. X
  41. Xinstall: rpn
  42. X    mv rpn ${DESTDIR}
  43. X    rm rpn.o
  44. X    mv rpn.1 ${DESTMAN}
  45. END_OF_FILE
  46. if test 159 -ne `wc -c <'Makefile'`; then
  47.     echo shar: \"'Makefile'\" unpacked with wrong size!
  48. fi
  49. # end of 'Makefile'
  50. fi
  51. if test -f 'README' -a "${1}" != "-c" ; then 
  52.   echo shar: Will not clobber existing file \"'README'\"
  53. else
  54. echo shar: Extracting \"'README'\" \(195 characters\)
  55. sed "s/^X//" >'README' <<'END_OF_FILE'
  56. XJust edit the makefile and do 'make install' and have everything
  57. Xhappen. any problems, please mail to mtymp01@ux.acss.umn.edu, or
  58. X...!rutgers!umn-cs!ux.acss!mtymp01, or mtymp01@UMNACUX.BITNET.
  59. X
  60. X
  61. END_OF_FILE
  62. if test 195 -ne `wc -c <'README'`; then
  63.     echo shar: \"'README'\" unpacked with wrong size!
  64. fi
  65. # end of 'README'
  66. fi
  67. if test -f 'rpn.1' -a "${1}" != "-c" ; then 
  68.   echo shar: Will not clobber existing file \"'rpn.1'\"
  69. else
  70. echo shar: Extracting \"'rpn.1'\" \(357 characters\)
  71. sed "s/^X//" >'rpn.1' <<'END_OF_FILE'
  72. X.TH RPN LOCAL "", ""
  73. X.AT 3
  74. X.SH NAME
  75. X.PP
  76. Xrpn - a reverse polish notation calculator
  77. X.SH SYNOPSIS
  78. Xrpn [-]
  79. X.PP
  80. X.SH DESCRIPTION
  81. X.PP
  82. X.I Rpn
  83. Xtakes the standard input and is a RPN calculator. If you want verbose
  84. Xoff, use 'rpn\ -'. For a more detailed description, type in 'help' from
  85. Xwithin rpn.
  86. X.SH BUGS
  87. X.PP
  88. X.SH MAN AUTHOR
  89. XNils McCarthy (mtymp01@ux.acss.umn.edu)
  90. END_OF_FILE
  91. if test 357 -ne `wc -c <'rpn.1'`; then
  92.     echo shar: \"'rpn.1'\" unpacked with wrong size!
  93. fi
  94. # end of 'rpn.1'
  95. fi
  96. if test -f 'rpn.c' -a "${1}" != "-c" ; then 
  97.   echo shar: Will not clobber existing file \"'rpn.c'\"
  98. else
  99. echo shar: Extracting \"'rpn.c'\" \(3850 characters\)
  100. sed "s/^X//" >'rpn.c' <<'END_OF_FILE'
  101. X/* rpn - a reverse polish notation calculator */
  102. X/* By Nils McCarthy with special thanks to:
  103. X    THE ZOO */
  104. X/* COPYRIGHT 1898 NILS MCCARTHY */
  105. X/* THIS CODE MAY BE DISTRIBUTED PROVIDED THIS HEADER IS KEPT WITH IT */
  106. X/* THIS CODE, EXCLUDING THIS HEADER MAY BE MODIFIED */
  107. X/* any questions about the copyright, mail to mtymp01@ux.acss.umn.edu */
  108. X
  109. X
  110. X#include <stdio.h>
  111. X
  112. Xint stacklength,*stack;
  113. X
  114. Xint top(void)
  115. X{
  116. X    return stack[stacklength-1];
  117. X}
  118. X
  119. Xpr(options)
  120. Xchar *options;
  121. X{
  122. X    if(*options==' ') options++;
  123. X    if(*options==NULL) {
  124. X        printf("%d\n",top());
  125. X        return;
  126. X    }
  127. X    if((stacklength-1-atoi(options))<0) fprintf(stderr,"ERROR: INVALID %s",
  128. X                "STACK ADDRESSING\n");
  129. X    printf("%d\n",stack[stacklength-1-atoi(options)]);
  130. X}
  131. X
  132. Xpush(thing)
  133. Xint thing;
  134. X{
  135. X    stacklength++;
  136. X    stack[stacklength-1]=thing;
  137. X    if(stacklength>=99) {
  138. X        fprintf(stderr,"ERROR: STACK OVERFLOW\n");
  139. X        /*exit(-1);*/
  140. X    }
  141. X}
  142. X
  143. Xint pop(void)
  144. X{
  145. X    int thing;
  146. X    thing=stack[stacklength-1];
  147. X    stacklength--;
  148. X    if(stacklength<0) {
  149. X          fprintf(stderr,"ERROR: STACK UNDERFLOW\n");    
  150. X          /*exit(-1);*/
  151. X      }
  152. X    return thing;
  153. X}
  154. X
  155. Xvoid add(void)
  156. X{
  157. X    int thing;
  158. X    thing=pop();
  159. X    push(pop()+thing);
  160. X}
  161. X
  162. Xvoid sub(void)
  163. X{
  164. X    int temp;
  165. X    temp=pop();
  166. X    push(pop()-temp);
  167. X}
  168. X
  169. Xvoid mul(void)
  170. X{
  171. X    int temp;
  172. X    temp=pop();
  173. X    push(pop()*temp);
  174. X}
  175. X
  176. Xvoid div(void)
  177. X{
  178. X    int temp;
  179. X    temp=pop();
  180. X    push(pop()/temp);
  181. X}
  182. X
  183. Xvoid mod(void)
  184. X{
  185. X    int temp;
  186. X    temp=pop();
  187. X    push(pop()%temp);
  188. X}
  189. X
  190. Xvoid not(void)
  191. X{
  192. X    push(~pop());
  193. X}
  194. X
  195. Xvoid xor(void)
  196. X{
  197. X    int thing;
  198. X    thing=pop();
  199. X    push(pop()^thing);
  200. X}
  201. X
  202. Xvoid and(void)
  203. X{
  204. X    int thing;
  205. X    thing=pop();
  206. X    push(pop()&thing);
  207. X}
  208. X
  209. Xvoid or(void)
  210. X{
  211. X    int thing;
  212. X    thing=pop();
  213. X    push(pop()|thing);
  214. X}
  215. X
  216. Xvoid help(void)
  217. X{
  218. X    printf("\n\nWELCOME TO RPN, a reverse polish notation calculator.\n");
  219. X    printf("the functions are as follows:\n");
  220. X    printf("add - add the top two numbers on the stack\n");
  221. X    printf("sub - subtract the top two numbers on the stack\n");
  222. X    printf("mul - multiply the top two numbers on the stack\n");
  223. X    printf("div - divide the top two numbers on the stack\n");
  224. X    printf("pop - discard the top element of the stack\n");
  225. X    printf("mod - take the remainder of the division of the top two numbers on the stack\n");
  226. X    printf("not - take the compliment of the top element on the stack\n");
  227. X    printf("help - display this help\n");
  228. X    printf("xor - exclusive or\n");
  229. X    printf("or - not-exclusive or\n");
  230. X    printf("and - bitwise and\n");
  231. X    printf("pr [number] - print the number [number] deep in the stack. defaults to 0\n");
  232. X    printf("sw - switch printing top of stack on/off\n");
  233. X    printf("(not that you'll ever need this, but:)\n");
  234. X    printf("end - terminate the progran\n\n");
  235. X}
  236. X
  237. Xmain(argc,argv)
  238. Xint argc;
  239. Xchar *argv[];
  240. X{
  241. X    char showstack=(~0); /* closest i can get to boolean */
  242. X    int stackspace[100];
  243. X    char input[100];
  244. X    stack=stackspace;
  245. X/* argument stuff */
  246. X    if(argc>1) showstack=0;
  247. X/* end of argument stuff (short, eh?) */
  248. X    printf("\nWELCOME TO RPN!!! (Rpn version 1) by Nils McCarthy\n\n");
  249. X    while (scanf("%s",input)!=EOF) {
  250. X        if(!strcmp(input,"add")) add();
  251. X        else if(!strcmp(input,"sub")) sub();
  252. X        else if(!strcmp(input,"mul")) mul();
  253. X        else if(!strcmp(input,"div")) div();
  254. X        else if(!strcmp(input,"pop")) pop();
  255. X        else if(!strcmp(input,"?"))  help();
  256. X        else if(!strcmp(input,"h")) help();
  257. X        else if(!strcmp(input,"sw")) showstack=~showstack;
  258. X        else if(!strncmp(input,"pr",2)) pr(input+2);
  259. X        else if(!strcmp(input,"help")) help(); 
  260. X        else if(!strcmp(input,"xor")) xor();
  261. X        else if(!strcmp(input,"or")) or();
  262. X        else if(!strcmp(input,"and")) and();
  263. X        else if(!strcmp(input,"x")) break;
  264. X        else if(!strcmp(input,"q")) break;
  265. X        else if(!strcmp(input,"exit")) break;
  266. X        else if(!strcmp(input,"quit")) break;
  267. X        else if(!strcmp(input,"end")) break;
  268. X        else push(atoi(input));
  269. X        if(showstack) {
  270. X            printf("\t\tstack top: %d\n",top());
  271. X            printf("\t\t\tstacksize: %d\n",stacklength);
  272. X        }
  273. X    }
  274. X    printf("Bye Bye, now! Please try rpn again!\n\n");
  275. X}
  276. END_OF_FILE
  277. if test 3850 -ne `wc -c <'rpn.c'`; then
  278.     echo shar: \"'rpn.c'\" unpacked with wrong size!
  279. fi
  280. # end of 'rpn.c'
  281. fi
  282. echo shar: End of shell archive.
  283. exit 0
  284.  
  285. ---Nils McCarthy---mtymp01@ux.acss.umn.edu---...!rutgers!umn-cs!ux.acss!mtymp01
  286.